In this work it will be analyzed the impact in quality of several parameters describing red wine. The dataset is curated by Udacity and comes from UCI repository https://archive.ics.uci.edu/ml/datasets/Wine+Quality and consists of 1599 sample data for Red wine https://docs.google.com/document/d/1qEcwltBMlRYZT-l699-71TzInWfk4W9q5rTCSvDVMpc/pub?embedded=true.
In Cortez et al. (2009) it is shown that the most imporant features for assessing Red Wine quality are:
sulphates
pH
total sulfur dioxide
# Load the Data
redwines <- read.csv('wineQualityReds.csv')
dim(redwines)
## [1] 1599 13
#names(redwines)
summary(redwines)
## X fixed.acidity volatile.acidity citric.acid
## Min. : 1.0 Min. : 4.60 Min. :0.1200 Min. :0.000
## 1st Qu.: 400.5 1st Qu.: 7.10 1st Qu.:0.3900 1st Qu.:0.090
## Median : 800.0 Median : 7.90 Median :0.5200 Median :0.260
## Mean : 800.0 Mean : 8.32 Mean :0.5278 Mean :0.271
## 3rd Qu.:1199.5 3rd Qu.: 9.20 3rd Qu.:0.6400 3rd Qu.:0.420
## Max. :1599.0 Max. :15.90 Max. :1.5800 Max. :1.000
## residual.sugar chlorides free.sulfur.dioxide
## Min. : 0.900 Min. :0.01200 Min. : 1.00
## 1st Qu.: 1.900 1st Qu.:0.07000 1st Qu.: 7.00
## Median : 2.200 Median :0.07900 Median :14.00
## Mean : 2.539 Mean :0.08747 Mean :15.87
## 3rd Qu.: 2.600 3rd Qu.:0.09000 3rd Qu.:21.00
## Max. :15.500 Max. :0.61100 Max. :72.00
## total.sulfur.dioxide density pH sulphates
## Min. : 6.00 Min. :0.9901 Min. :2.740 Min. :0.3300
## 1st Qu.: 22.00 1st Qu.:0.9956 1st Qu.:3.210 1st Qu.:0.5500
## Median : 38.00 Median :0.9968 Median :3.310 Median :0.6200
## Mean : 46.47 Mean :0.9967 Mean :3.311 Mean :0.6581
## 3rd Qu.: 62.00 3rd Qu.:0.9978 3rd Qu.:3.400 3rd Qu.:0.7300
## Max. :289.00 Max. :1.0037 Max. :4.010 Max. :2.0000
## alcohol quality
## Min. : 8.40 Min. :3.000
## 1st Qu.: 9.50 1st Qu.:5.000
## Median :10.20 Median :6.000
## Mean :10.42 Mean :5.636
## 3rd Qu.:11.10 3rd Qu.:6.000
## Max. :14.90 Max. :8.000
# New variables
#redwines$quality.factor <- factor(redwines$quality)
redwines$quality.cat <- NA
redwines$quality.cat <- ifelse(redwines$quality>=7, 'good','medium')
redwines$quality.cat <- ifelse(redwines$quality<=4, 'bad',redwines$quality.cat) # if not, leave the previous values
redwines$quality.cat <- factor(redwines$quality.cat, levels = list('bad', 'medium','good')) # set the order!
print("Variables after dividing into quality groups")
## [1] "Variables after dividing into quality groups"
str(redwines) #summary of values for each variable
## 'data.frame': 1599 obs. of 14 variables:
## $ X : int 1 2 3 4 5 6 7 8 9 10 ...
## $ fixed.acidity : num 7.4 7.8 7.8 11.2 7.4 7.4 7.9 7.3 7.8 7.5 ...
## $ volatile.acidity : num 0.7 0.88 0.76 0.28 0.7 0.66 0.6 0.65 0.58 0.5 ...
## $ citric.acid : num 0 0 0.04 0.56 0 0 0.06 0 0.02 0.36 ...
## $ residual.sugar : num 1.9 2.6 2.3 1.9 1.9 1.8 1.6 1.2 2 6.1 ...
## $ chlorides : num 0.076 0.098 0.092 0.075 0.076 0.075 0.069 0.065 0.073 0.071 ...
## $ free.sulfur.dioxide : num 11 25 15 17 11 13 15 15 9 17 ...
## $ total.sulfur.dioxide: num 34 67 54 60 34 40 59 21 18 102 ...
## $ density : num 0.998 0.997 0.997 0.998 0.998 ...
## $ pH : num 3.51 3.2 3.26 3.16 3.51 3.51 3.3 3.39 3.36 3.35 ...
## $ sulphates : num 0.56 0.68 0.65 0.58 0.56 0.56 0.46 0.47 0.57 0.8 ...
## $ alcohol : num 9.4 9.8 9.8 9.8 9.4 9.4 9.4 10 9.5 10.5 ...
## $ quality : int 5 5 5 6 5 5 5 7 7 5 ...
## $ quality.cat : Factor w/ 3 levels "bad","medium",..: 2 2 2 2 2 2 2 3 3 2 ...
#unique(redwines$quality.cat)
X: is the row idquality: range is between 3 and 8quality.cat: generated to group quality in ranges: 0-4 -> BAD, 4-7 -> MEDIUM, 7-10 -> GOOD. More information in the section “Analysis”.In this section it will be analyzed each of the variables describing the wines.
The distribution of wine shows that most of wines have a quality between 5-6 points.
Figure 3.1: Quality distribution
In the next plot it is deduced that the quality of the wine is directly proportional to the fixed acidity and acid levels and inversely proportional to volatile acidity.
Figure 3.2: Fixed and volatile acidity distributions
The plot of residual suggar shows that the better the wine the higher the residual sugar levels.
Figure 3.3: Residual sugar distribution
In the plot of the chlorides, it can be onserved that the better the wine, the lower the chloride levels. For the sulfur dioxide, either the free or the total sulfur dioxide, high levels are indicator of medium quality, whereas bad and good wine have the same low amount of sulfure dioxide.
Figure 3.4: Chlorides and sulfur dioxide distributions
The next plot shows that, generally, the lower the density, the better the quality of the wine. ALso, low pH levels are sign of beter quality. The higher the sulphates level, the better quality of the wine and also, good wines have higher amount of alcohol.
Figure 3.5: Density, pH, sulphates and alcohol distributions
There are 1599 red wines with 12 features (fixed.acidity, volatile.acidity, citric.acid, residual.sugar, chlorides, free.sulfur.dioxide, total.sulfur.dioxide, density, pH, sulphates, alcohol and quality). The variables quality is converted in a factor variable (adding a new variable named quality.cat) with the following levels:
| BAD | MEDIUM | GOOD | |
|---|---|---|---|
quality.cat |
[0,4] | (4,7) | [7,10] |
Other observations:
| Min. | 1st Qu. | Median | Mean | 3rd Qu. | Max. | |
|---|---|---|---|---|---|---|
redwines.quality |
3 | 5 | 6 | 5.636 | 6 | 8 |
Table 3.1 shows that the mean quality of the red wines is 5.636 and the median is 6. Q1 division corresponds to 5 and Q3 to 6, hence, 50% of the data lays within 5-6 range of quality, this is the level MEDIUM.
The main feature of interest in this dataset is the quality of the wine.
In Cortez et al. (2009) it is shown that the most imporant features for assessing Red Wine quality are: sulphates, pH and total sulfur dioxide. An analysis will be performed in order to certify whether these are the most importart features for the analysis or others are.
I did not create any new variables to support the analysis since the amount of information available is enough to assess the quality of the wine.
Most of the features had a normal distribution. Some of the them had quite skewed distributions and many oultiers and I performed a log10 tranformation in order to have a better view. The efect of this transformation can be observed in figure 3.6, where charateristics of plots in the left side are seen better after the x axis is transformed.
Figure 3.6: Residual sugar and Chlorides distributions with original and log10() transformed axis
In this section it will be perfomed an analysis of the features vs the quality ranges.
In the next plot (fig.4.1) it is deduced that the quality of the wine is directly proportional to the fixed acidity and acid levels and inversely proportional to volatile acidity.
Figure 4.1: Fixed and volatile acidity distributions per quality segment
The plot of residual suggar (4.2) shows that the better the wine the higher the residual sugar levels.
Figure 4.2: Residual sugar distribution per quality segment
In the plot of the chlorides, it can be onserved that the better the wine, the lower the chloride levels. For the sulfur dioxide, either the free or the total sulfur dioxide, high levels are indicator of medium quality, whereas bad and good wine have the same low amount of sulfure dioxide.
Figure 4.3: Chlorides and sulfur dioxide distributions per quality segment
The next plot shows that, generally, the lower the density, the better the quality of the wine. Also, low pH levels are sign of beter quality. The higher the sulphates level, the better quality of the wine and also, good wines have higher amount of alcohol.
Figure 4.4: Density, pH, suplhates and alcohol distributions per quality segment
A good way to see how to variables are related is using the correlation. In this sectio it will be calculated the correlation between the features of red wine and the strongest will be analyzed.
In the next figure (fig.4.5) it is shown the correlation factor between the different variables in the Red Wines dataset. Significant correlation values (\(|\rho|>0.3\)) are highlighted with stronger color density the higher the correlation value.
For the purpose of this analysis, the focus will be placed in the relationship between quality and other variables.
Figure 4.5: Correlation bewteen Red Wine’s dataset features
Figure 4.6: Correlation vs Quality
From the general correlation matrix (fig.4.5), three main variables can be selected due to their high correlation with quality: alcohol (\(\rho=\) 0.48 ), volatile.acidity (\(\rho=\) -0.39 ) and sulphates (\(\rho=\) 0.25 ). In order to see if they are suitable to perform an analysis let us explore the correlation between them and also the distribution of samples in bivariate plots in figure 4.7.
Figure 4.7: Correlation between alcohol, volatile acidity and sulphates
From plot 4.7 it is observed that there is a strong relationship between the sulphates and volatile acidity, and less strong between alcohol and volatile acidity.
Let us now explore the three variables suggested in Cortez et al. (2009) as the most imporant features for assessing Red Wine quality are: sulphates, pH and total sulfur dioxide.
Figure 4.8: Correlation between total sulfur dioxide, pH and sulphates
Figure 4.7 shows that the strongest relationship is between sulphates and pH (\(\rho=-0.2\)) followed by pH-total sulfur dioxide.
From figure 4.8 and 4.7 it can be concluded the varibles in the second figure (sulphates, pH and total sulfur dioxide), the ones suggested by Cortez et al. (2009) are less correlated between them and lead to a more accurate analysis.
For the three main variables (fig.4.9) influencing the quality of wine the correlation is:
Figure 4.9: Sulphates, pH and total sulfur dioxide vs quality
Figure 4.5 shows the correlation between the variables in the dataset. The strongest are these pairs: citric acid-fixed acidity, total sulfur dioxide - free sulfur dioxide, density- fixed acidity and pH - fixed acidity.
It is logical that citric acid influences fixed acidity, as does free sulfur dioxide with total sulfur dioxide. Ph is known to be related to the acid levels, so no surprise here. I did not know there was such a strong relationship between density and fixed acidity.
Figure 4.10 shows the relationship between fixed acidity and density, and between fixed acidity and pH. Fixed acidity and density have a strong positive correlation (\(\rho=\)rround(subset(corr_general_R, (corr_general_R$y=='fixed.acidity')&(corr_general_R$x=='density'), select = 'label' ),2)) and fixed acidity and pH show a strong negative correlation (\(\rho=\) -0.68).
Figure 4.10: Density and pH vs fixed acidity
Figure 5.1: Sulphates, pH and total sulfur dioxide relationship vs quality
Figure 5.2: Fixed acidity vs pH, fixed acidity vs citric acid and volatile acidity vs citric acid per quality range
Figure 5.1 shows the 2D density distribution of the three variables (compared in pairs) that influence the most the quality of the wine. It is interesting how the centers seem to displace in a linear manner.
For the case of sulphates-pH there is a negative dependence, coincident with the value of the correlation for these two variables \(\rho=\) -0.2.
For the case of sulphates-total sulfur dioxide there seems to be no dependence, what is coincident with the value of the correlation for these two variables \(\rho=\) 0.04.
For the case of pH-total sulfur dioxide there seems to be no dependence, what is coincident with the value of the correlation for these two variables \(\rho=\) -0.07.
Figure 5.2 shows the relationship between the highest values of correlation in figure the matrix of correlation in figure 4.5. In this case the variables pairs selected were: fixed acidity vs pH, fixed acidity vs citric acid and volatile acidity vs citric acid, al with different colors per quality range.
For the case of fixed acidity vs pH there is a negative dependence, coincident with the value of the correlation for these two variables \(\rho=\) -0.68.
For the case of fixed acidity vs citric acid there seems to be a strong dependence, what is coincident with the value of the correlation for these two variables \(\rho=\) 0.67.
For the case of volatile acidity vs citric acid there seems to be no dependence, what is coincident with the value of the correlation for these two variables \(\rho=\) -0.55.
I found that there is no significative correlation between pH-total sulfur dioxide. Also, I found that usually, higher quality wine is related to higher levels of citric acid.
I created a multiple linear model with the three main features: sulphates, pH and total sulfur dioxide.
The linear model used takes log10 of each variable to make the model. In figure 6.1 it is shown how the overall error is reduced. However, when looking to the error per quality range (fig. 6.2), the error is higher for extreme values of quality. This makes sense, since the most abundant group is the one with medium quality and hence, the model is more accurate with this segment.
Figure 6.1: Error by linear model
Figure 6.2: Error by linear model
The model has the following strengths and limitations:
strengths: simple and limited to most influencing variables.
limitations: not all variables are included, the combination of variables is linear and the quality is set by humans with different criteria.
Figure 7.1: Correlation matrix
Figure 7.1 shows the correlation coefficient between the variables in the Red Wines dataset.
Figure 7.2: Sulphates, pH and total sulfur dioxide vs quality
Figure 7.2 shows the distribution per quality value of for the three main variables influencing the quality of wine, with a line blue showing the linear model of the relation quality-feature and a red one showing the mean value of the feature per quality value.
Figure 7.3: Citric acidity vs quality
Figure 7.3 shows the distribution of citric acid per quality range and also the box plot per quality range. It can be observed how higher quality wines have more levels of citic acid than lower quality wine. I really liked this conclusion, and that is why it is included here, because I always thought it would be the opposite, that lower quality wine would be the more acid.
With this work I have realized how important is to investigate a dataset before starting to make a model or just to extract conclusions. Making an Exploratory Data Analysis (EDA) is an iterative work trying to find the best way to explain relationships between variables and distribution of data and then communicate the result in a clear and concise way.
I found that the correlation matrix is crucial for continuous variables, giving the lead on where you have to look closer at. It is also important to know the the data, each variable and for that, the plotting the distributions is important.
Sulphates, pH and total sulfur dioxide are the most important features to explaing the Red Wine quality, as suggested by Cortez et al. (2009). However, the correlation matrix shows that it is alcohol, sulphates and volatile acidity the most correlated with quality. After exploring the correlation coefficient between them I could see that “alcohol, sulphates and volatile acidity” set is more correlated between the variables than the “sulphates, pH and total sulfur dioxide” set. From quality exploration it is noticeable that there are no records for wines with quality below 3 or above 8.
This EDA of the Red Wine quality has given me the opportunity to approach a dataset using R and draw conclusions from data. This experience will be helpful in the future when approaching simiar problems.
Cortez, Paulo, António Cerdeira, Fernando Almeida, Telmo Matos, and José Reis. 2009. “Modeling Wine Preferences by Data Mining from Physicochemical Properties.” Decision Support Systems 47 (4). Elsevier: 547–53.
Medina, Jason. –. “P4 Explore and Summarize Data: Red Wine Eda.” https://rpubs.com/jasonmedina/220283.
Thomas, Iwan. n.d. “EDA of Red Wine Quality Dataset.” https://github.com/IwanThomas/Udacity-Data-Analysis-Nanodegree/blob/master/Project-4/RedWineRMD.Rmd.
“Wine Quality Analysis.” –. https://rpubs.com/sanmen/24803.